package com.coocaa.typhoon.api.v1.milvus.convert;

import com.coocaa.typhoon.api.third.volcengine.util.BaseValidatorUtil;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2025/9/25 10:48
 * @description
 */
@UtilityClass
public class MilvusFilterConverter {

    public static String getFilter(String userId, List<String> fileTypeList, long startShootTime, String location, long endShootTime) {
        StringBuilder filterBuilder = new StringBuilder();
        filterBuilder.append("status == 1 && ");
        if (StringUtils.isNotBlank(userId)) {
            filterBuilder.append("user_id == '").append(userId).append("' && ");
        }
        buildFileTypeFilter(fileTypeList, filterBuilder);
//        if (null != hasMultiTurnFlow) {
//            filterBuilder.append("has_multi_turn_flow == ").append(hasMultiTurnFlow).append(" && ");
//        }
//        if (questionMethod != null && questionMethod > 0) {
//            filterBuilder.append("ques_method == ").append(questionMethod).append(" && ");
//        }
        if (StringUtils.isNotBlank(location)) {
            filterBuilder.append("location == '").append(location).append("' && ");
        }
        if (BaseValidatorUtil.isTimeCanFilter(startShootTime) && BaseValidatorUtil.isTimeCanFilter(endShootTime)) {
            filterBuilder.append("(start_time >= ").append(startShootTime)
                    .append(" && end_time <= ").append(endShootTime).append(")");
        } else if (BaseValidatorUtil.isTimeCanFilter(startShootTime) && !BaseValidatorUtil.isTimeCanFilter(endShootTime)) {
            filterBuilder.append("shoot_time  >= ").append(startShootTime);
        } else if (!BaseValidatorUtil.isTimeCanFilter(startShootTime) && BaseValidatorUtil.isTimeCanFilter(endShootTime)) {

            filterBuilder.append("shoot_time  <= ").append(endShootTime);
        }
        // 去掉末尾多余的 " && "（包括前后空格）
        return filterBuilder.toString().replaceAll("\\s*&&\\s*$", "");
    }

    private static void buildFileTypeFilter(List<String> fileTypeList, StringBuilder filterBuilder) {
        if (CollectionUtils.isEmpty(fileTypeList)) {
            return;
        }
        filterBuilder.append("file_type in [");
        int size = fileTypeList.size();
        int i = 1;
        for (String fileType : fileTypeList) {
            filterBuilder.append("'").append(fileType).append("'");
            if (i < size) {
                filterBuilder.append(",");
            }
            i++;
        }
        filterBuilder.append("] && ");
    }
}
